<?php

namespace catchAdmin\disease\controller;

use app\common\model\Disease;
use catcher\CatchResponse;
use catcher\base\CatchRequest;
use catcher\base\CatchController;
use app\common\model\DiseaseType as DisTypeModel;
use app\common\model\Disease as DiseaseModel;
use catcher\exceptions\FailedException;
use catchAdmin\disease\request\DiseaseTypeRequest;
use think\db\Query;

class DiseaseType extends CatchController
{
    /**
     * 列表
     *
     * @time 2020年01月09日
     * @return \think\response\Json
     */
    public function index(): \think\response\Json
    {
        return CatchResponse::paginate((new DisTypeModel())->getLists());
    }

    /**
     * 新增
     * @param DiseaseTypeRequest $request
     * @return \think\response\Json
     */
    public function save(DiseaseTypeRequest $request): \think\response\Json
    {
        $req = $request->post();
        $req['deleted_at'] = 0;
        $req['creator_name'] = request()->user()->username;
        return CatchResponse::success((new DisTypeModel())->addData($req));
    }

    /**
     * 更新
     * @param string $id 主键
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function update(string $id, CatchRequest $request): \think\response\Json
    {
        $req = $request->post();
        $req['creator_name'] = request()->user()->username;
        return CatchResponse::success((new DisTypeModel())->saveData($id, $req));
    }

    /**
     * 删除
     * @param string $id
     * @return \think\response\Json
     */
    public function delete(string $id): \think\response\Json
    {
        return CatchResponse::success((new DisTypeModel())->destroy($id));
    }

    /**
     * 模糊查询疾病
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function getLikeDisease(CatchRequest $request): \think\response\Json
    {
        $req = $request->post();
        if (!isset($req['illnessno']) || empty($req['illnessno'])) {
            throw new FailedException("请输入疾病编码");
        }

        $keyword = str_replace('+', '\+', $req['illnessno']);
        $where[] = ['illnessno', 'like', $keyword];
        $ret = (new DiseaseModel())->getConditionData($where, 'id,illnesscode,name,illnessno');
        return CatchResponse::success($ret);
    }

    /**
     * 获取病种
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function getDiseaseType(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();
        $where = [];
        if (!isset($req['disease_type_name']) || empty($req['disease_type_name'])) {
            throw new FailedException("请输入病种名称");
        }
        $where[] = ['diseaseTypeName', '=', ['$regex' => '^' . $req['disease_type_name']]];
        $ret = (new DisTypeModel())->getConditionData($where, ["id", "diseaseTypeName"]);
        return CatchResponse::success($ret);
    }

    public function getDiseaseTypeList(CatchRequest $request)
    {
        $paginate = $request->param("limit", 0, "int");
        if ($paginate) {
            $result = (new \app\common\model\DiseaseType())->mongoSearch()->paginate($paginate);

            return CatchResponse::paginate($result);
        } else {
            $result = (new \app\common\model\DiseaseType())->mongoSearch()->select();
            return CatchResponse::success($result);
        }

    }
}